import * as commonService from '../services/SSRS_Report';

export default {
    namespace: 'SSRS_Report',
    state: {
        controls: {},
        paramValues: {},
        url: null     //"http://60.161.0.8:81/DataReport/ReportViewForGo.aspx"
    },
    reducers: {
        saveControls(state, { payload: { permID, conts, url } }) {
            let controls = { ...state.controls, [permID]: conts }
            let pVals = {}
            conts.map(cont => {
                pVals[cont.name] = cont.defaultValue
            });
            let paramValues = { ...state.paramValues, [permID]: pVals }
            return { ...state, controls, paramValues, url };
        },
        saveParamValues(state, { payload: { permID, values} }) {
            const paramValues = { ...state.paramValues, [permID]: values }
            return { ...state, paramValues};
        },
    },
    effects: {
        *fetchControls({ payload: { permID } }, { call, put }) {
            const conts = yield call(commonService.fetchControls, permID);
            const url = yield call(commonService.fetchControlsUrl);
            if (conts) {
                yield put({
                    type: 'saveControls',
                    payload: {
                        permID,
                        conts,
                        url
                    },
                });
            }
        },
    }
}